package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyComment;
import cn.wolfcode.trip.base.domain.DailyType;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.DailyQueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/daily")
public class DailyController {
    @Autowired
    private IDailyService dailyService;

    //查询所有的推荐日报
    @GetMapping(value = "/{state}")
    @ResponseBody
    public Object getDailies(DailyQueryObject qo) {
        PageInfo<Daily> pageInfo = dailyService.query(qo);
        return  pageInfo;
    }

    //查询单个日报(内容)
    @GetMapping
    @ResponseBody
    public Object getDaily(Long id) {
        Daily daily = dailyService.get(id);
        return  daily;
    }

    //根据类型获取所有的日报
    @GetMapping(value = "/type")
    @ResponseBody
    public Object getDailyType() {
        List<DailyType> dailyTypes = dailyService.getDailyType();
        return  dailyTypes;
    }

    //查询所有的评论
    @GetMapping(value = "/comments")
    @ResponseBody
    public Object getComments(Long dailyId) {
        List<DailyComment> comments = dailyService.getComments(dailyId);
        return  comments;
    }

    @PostMapping(value = "/comments")
    @ResponseBody
    public Object saveComments(DailyComment dailyComment) {
        JSONResult result = new JSONResult();
        try {
            User user = UserContext.getCurrentUser();
            dailyComment.setUser(user);
            dailyService.saveComments(dailyComment);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("失败");
        }
        return  result;
    }

}